package com.green.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ClassUtil {
	/**
	 * 复制类属性，该方法只实现本类中属性拷贝，如需拷贝父类属性 可采用递归的方式
	 * 
	 * @author hgpeng
	 * @param <T>
	 * @param clazz
	 * @param source
	 * @param target
	 */
	public static <T> void copyPropertyToOtherClass(Class<T> clazz, Object source, Object target) {
		if (source == null || target == null) {
			throw new RuntimeException("源对象与目标对象都不能为空");
		}
		Field[] fields = clazz.getDeclaredFields();
		for (Field eachField : fields) {
			String fieldName = eachField.getName();
			// serialVersionUID没有对应的set方法
			if (fieldName.equals("serialVersionUID")) {
				continue;
			}
			String str = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			String setMethodName = "set" + str;
			String getMethodName = "get" + str;
			try {
				Method setMethod = clazz.getMethod(setMethodName, new Class[] { eachField.getType() });
				Method getMethod = clazz.getMethod(getMethodName, new Class[] {});
				// 获取源类属性值
				Object o = getMethod.invoke(source, new Object[] {});
				if (o != null) {
					setMethod.invoke(target, new Object[] { o });
				}
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}

	}

	public static Object newInstance(String clsName) {
		try {
			Class<?> cls = Class.forName(clsName);
			return cls.newInstance();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

}
